import ConstStrings from "./datas/strings";
import GameConfig from "./GameConfig";
import ScreenPositionUtil from "./ScreenPositionUtil";

export default class CandidateItemView extends Laya.Box {
    constructor() {
        super();
        // this,this.bgColor = "#ff000044"
        this.boxMarginLeft = ScreenPositionUtil.getPostionInStage(ConstStrings.boxMarginLeftRatio ,"x");
        // debugger
		this.boxMarginTop = ConstStrings.candidateStartY;
		// this.boxMarginBottom = ScreenPositionUtil.getPostionInStage(ConstStrings.boxMarginBottomRatio ,"y");
		this.boxMarginBottom = ConstStrings.boxMarginBottomDis * GameConfig.inPr;

        this.WID = (Laya.stage.width - this.boxMarginLeft * 2) / 2;
        this.HEI = (Laya.stage.height - this.boxMarginTop - this.boxMarginBottom) / 4;

        this.listItemSrcWidth = Math.min(this.WID , this.HEI) ;
        this.listItemSrcHeight = this.listItemSrcWidth;

        this.size(this.WID, this.HEI);
        this.pivot(this.WID * 0.5, this.HEI * 0.5);
        this.pos(this.WID * 0.5, this.HEI * 0.5);

        this.rootNode = new Laya.Sprite;
        this.rootNode.size(this.listItemSrcWidth, this.listItemSrcHeight);
        this.rootNode.pivot(this.rootNode.width * 0.5, this.rootNode.height * 0.5);
        // this.rootNode.pos(this.WID * 0.5, this.HEI * 0.5);
        this.rootNode.pos(this.listItemSrcWidth * 0.5, this.listItemSrcHeight * 0.5);
        this.addChild(this.rootNode);

        // outline 人
        this.srcOutlineImg = new Laya.Sprite();
        this.srcOutlineImg.size(this.listItemSrcWidth * 0.92, this.listItemSrcHeight * 0.92);
        this.rootNode.addChild(this.srcOutlineImg);
        this.srcOutlineImg.pivot(this.srcOutlineImg.width * 0.5, this.srcOutlineImg.height * 0.5);
        this.srcOutlineImg.pos(this.WID * 0.5, this.HEI * 0.5);
        this.srcOutlineImg.loadImage(ConstStrings.candidateOutlineImg);
        this.srcOutlineImg.alpha = 0;

        // 背景白片
        this.srcBgImg = new Laya.Sprite();
        this.srcBgImg.size(this.listItemSrcWidth * 0.92, this.listItemSrcHeight * 0.92);
        
        this.rootNode.addChild(this.srcBgImg);
        this.srcBgImg.pivot(this.srcBgImg.width * 0.5, this.srcBgImg.height * 0.5);
        this.srcBgImg.pos(this.WID * 0.5, this.HEI * 0.5);
        this.srcBgImg.loadImage(ConstStrings.candidateBgImg)

        // 鞋子
        this.srcImg = new Laya.Sprite();
        this.srcImg.size(this.listItemSrcWidth, this.listItemSrcHeight);
        this.srcBgImg.addChild(this.srcImg);
        this.srcImg.pivot(this.listItemSrcWidth * 0.5, this.listItemSrcHeight * 0.5);
        this.srcImg.pos(this.listItemSrcWidth * 0.5, this.listItemSrcWidth * 0.5);


        this.unSelectImg = new Laya.Sprite();
        this.unSelectImg.size(this.listItemSrcWidth * 0.94, this.listItemSrcHeight * 0.94);
        this.rootNode.addChild(this.unSelectImg);
        this.unSelectImg.pivot(this.unSelectImg.width * 0.5, this.unSelectImg.height * 0.5);
        this.unSelectImg.pos(this.WID * 0.5, this.HEI * 0.5);
        this.unSelectImg.loadImage(ConstStrings.candidateUnselectedImg);
        this.unSelectImg.alpha = 1;

        this.selectImg = new Laya.Sprite();
        this.selectImg.size(this.listItemSrcWidth * 0.94, this.listItemSrcHeight * 0.94);
        this.rootNode.addChild(this.selectImg);
        this.selectImg.pivot(this.selectImg.width * 0.5, this.selectImg.height * 0.5);
        this.selectImg.pos(this.WID * 0.5, this.HEI * 0.5);
        this.selectImg.loadImage(ConstStrings.candidateSelectedImg);
        this.selectImg.alpha = 0;

        this.wrongSelectImg = new Laya.Sprite();
        this.wrongSelectImg.size(this.listItemSrcWidth * 0.94, this.listItemSrcHeight * 0.94);
        this.rootNode.addChild(this.wrongSelectImg);
        this.wrongSelectImg.pivot(this.wrongSelectImg.width / 2, this.wrongSelectImg.height / 2);
        this.wrongSelectImg.pos(this.WID * 0.5, this.HEI * 0.5);
        this.wrongSelectImg.loadImage(ConstStrings.candidateWrongImg);
        this.wrongSelectImg.alpha = 0;

        this.selectAni = null;
    }

    setSrcImg(src) {
        this.srcImg.loadImage(src);
    }

    isSelectImgVisible() {
        let _self = this;
        if (this.dataSource.needAni) { // 需要动画
            if (this.dataSource.needAniType == "unselect") {
 
                Laya.Tween.to(_self.srcBgImg, { alpha: 1 }, 300, Laya.Ease.quartOut,null,300);
                
                Laya.Tween.to(_self.unSelectImg, { alpha: 1 }, 300, Laya.Ease.quartOut, null, 0);
                Laya.Tween.to(_self.selectImg, { alpha: 0 }, 300, Laya.Ease.quartOut, null, 300);

                Laya.Tween.to(_self.rootNode, { scaleX: 0. }, 300, Laya.Ease.strongIn, null, 0);
                Laya.Tween.to(_self.rootNode, { scaleX: 1 }, 300, Laya.Ease.strongIn, null, 400);

                Laya.Tween.to(_self.srcOutlineImg, { alpha: 0 }, 300, Laya.Ease.strongIn, null, 100);
                
            }
            else if (this.dataSource.needAniType == "select") {
                Laya.Tween.to(_self.rootNode, { scaleX: 1.05, scaleY:1.05 }, 200, Laya.Ease.strongIn, null, 0);

                Laya.Tween.to(_self.srcBgImg, { alpha: 0 }, 300, Laya.Ease.quartOut,null,400);
                Laya.Tween.to(_self.unSelectImg, { alpha: 0 }, 300, Laya.Ease.quartOut, null, 200);
                Laya.Tween.to(_self.selectImg, { alpha: 1 }, 300, Laya.Ease.quartOut, null, 200);
                Laya.Tween.to(_self.selectImg, { alpha: 0 }, 300, Laya.Ease.quartOut, null, 500);

                Laya.Tween.to(_self.rootNode, { scaleX: 0. }, 300, Laya.Ease.strongIn, null, 200);
                Laya.Tween.to(_self.rootNode, { scaleX: 1 }, 300, Laya.Ease.strongIn, null, 600);

                Laya.Tween.to(_self.srcOutlineImg, { alpha: 1 }, 300, Laya.Ease.strongIn, null, 600);
                Laya.Tween.to(_self.rootNode, { scaleY:1. }, 100, Laya.Ease.strongIn, null, 600);

            } else if(this.dataSource.needAniType == "wrong") {
                _self.rotation = 0.0;
                Laya.Tween.to(_self.rootNode, { scaleX: 1.05, scaleY:1.05 }, 200, Laya.Ease.strongIn, null, 0);

                Laya.Tween.to(_self.srcBgImg, { alpha: 1 }, 200, Laya.Ease.quartOut,null,200);
                Laya.Tween.to(_self.unSelectImg, { alpha: 0 }, 200, Laya.Ease.quartOut, null, 200);
                Laya.Tween.to(_self.selectImg, { alpha: 0 }, 200, Laya.Ease.quartOut, null, 200);
                Laya.Tween.to(_self.wrongSelectImg, { alpha: 1 }, 200, Laya.Ease.quartOut, null, 200);
                Laya.Tween.from(_self, { rotation: 5 }, 400, Laya.Ease.elasticOut, null, 200);
                Laya.Tween.to(_self.wrongSelectImg, { alpha: 0 }, 200, Laya.Ease.quartOut, null, 500);
                Laya.Tween.to(_self.unSelectImg, { alpha: 1 }, 200, Laya.Ease.quartOut, null, 500);

                Laya.Tween.to(_self.rootNode, { scaleX: 1., scaleY:1. }, 200, Laya.Ease.strongIn, null, 500);

            }

            this.dataSource.needAni = false;
        }
    }

    getShoeAniInfo() {
        let gPos = this.srcImg.localToGlobal(new Laya.Point(this.listItemSrcWidth * 0., this.listItemSrcWidth * 0.))
        return {
            x: gPos.x,
            y: gPos.y,
            width :this.srcImg.width, 
            height:this.srcImg.height,
            src:this.dataSource.src
        }
    }
}